library EMTransmitter initializer init requires LegacySystem

    globals
        private unit udg_EMTarget = null
        private integer EM_in_process = 0
    endglobals
    
    private function SentryCheck takes nothing returns boolean
        local integer i = GetUnitTypeId(GetFilterUnit())
        return i=='hwtw' or i=='hatw' or i=='o001'
    endfunction

    private function EMRadiationConditions takes nothing returns boolean
        local integer i = GetSpellAbilityId()
        return i=='A05V' or i == 'A096'
    endfunction

    private function CancelEM takes nothing returns boolean
        if udg_EMTarget != null and EM_in_process <= 0 then
            call ALICEText(2.0, "Target EM cancelled.") 
            set udg_EMTarget = null
            set EM_in_process = 0
		elseif EM_in_process > 0 then
			call ALICEText(2.0, "EM Radiation deployment in process, unable to cancel.") 
        endif
        return false
    endfunction

    private function EMRadiation takes nothing returns nothing
        local real x
        local real y
        local unit u = GetTriggerUnit()
        local unit u2
        local location l = GetUnitLoc(u)
        local location l2
        local player p = GetOwningPlayer(u)
		
        local group g = AMGetUnitsInRange(800.0,l,Condition(function SelectClassA))
        local group g2 = AMGetUnitsInRange(500.0,l,Condition(function SentryCheck))
		

        if udg_EMTarget!= null and FirstOfGroup(g2)!=null and GetWidgetLife(udg_EMTarget)>0.405 then
            set u2 = FirstOfGroup(g2)
            if GetUnitState(u2,UNIT_STATE_MANA)<0.5 then
                call IssueImmediateOrder(u,"holdposition")
            else
                set EM_in_process = EM_in_process + 1
                set x = GetUnitX(udg_EMTarget)
                set y = GetUnitY(udg_EMTarget)
                //call SetCameraQuickPosition(x,y)
                call PingMinimapEx(x,y,5.0,98,115,202,false)
                call SetUnitState( u2, UNIT_STATE_MANA, RMaxBJ(0,GetUnitState(u2, UNIT_STATE_MANA)-1) )
                call ALICEText(2.0, "Activating EM radiation module.." )
                call PolledWait(1.)
                set l2 = GetUnitLoc(udg_EMTarget)
                call PlaySoundAtPointBJ( gg_snd_EMRadiation, 100, l2, 0 )
                call UnitAddAbility(udg_EMTarget,'A05F')
                call SetUnitMoveSpeed( udg_EMTarget, GetUnitDefaultMoveSpeed(udg_EMTarget)*0.4 )
                call RemoveLocation(l2)
                call SetUnitVertexColorBJ( udg_EMTarget, 90, 90, 90, 0 )
                call SetUnitTimeScalePercent(udg_EMTarget, 150.0)
                call PolledWait(1.)
                call SetUnitVertexColorBJ( udg_EMTarget, 80, 80, 80, 0 )
                call SetUnitTimeScalePercent(udg_EMTarget, 200.0)
                call TriggerSleepAction(1.)
                call SetUnitVertexColorBJ( udg_EMTarget, 70, 70, 70, 0 )
                call SetUnitMoveSpeed( udg_EMTarget, GetUnitDefaultMoveSpeed(udg_EMTarget)*0.25 )
                call SetUnitTimeScalePercent(udg_EMTarget, 250.0)
                call PolledWait(1.)
                call SetUnitVertexColorBJ( udg_EMTarget, 50, 50, 50, 0 )
                call SetUnitTimeScalePercent(udg_EMTarget, 300.0)
                call TriggerSleepAction(1.)
                call UnitRemoveAbility(udg_EMTarget,'A05F')
                if GetWidgetLife(udg_EMTarget)>0.405 then
                    call UnitDamagePoint( udg_Residence41, 0, 250.0, GetUnitX(udg_EMTarget), GetUnitY(udg_EMTarget), 20000.0, true, false, ATTACK_TYPE_SIEGE, DAMAGE_TYPE_NORMAL, WEAPON_TYPE_WHOKNOWS )
                endif
                
                call PolledWait(5.)

				set EM_in_process = EM_in_process - 1
                if udg_EMTarget!= null and GetWidgetLife(udg_EMTarget)>0.405 and EM_in_process == 0 then
					call SetUnitTimeScalePercent(udg_EMTarget, 100.0)
                    call SetUnitMoveSpeed( udg_EMTarget, GetUnitDefaultMoveSpeed(udg_EMTarget) )
                endif

				if udg_EMTarget != null and EM_in_process <= 0 and GetWidgetLife(udg_EMTarget)<=0.405 then
					call ALICEText(2.0, "Target EM neutralized.") 
					set udg_EMTarget = null
					set EM_in_process = 0
				endif
            endif
        elseif udg_EMTarget!= null and FirstOfGroup(g2)==null and GetWidgetLife(udg_EMTarget)>0.405 then
            call DisplayTimedTextToPlayer( p,0.0,0.0, 5.0, "No suitable EM transmitters within range." )
        elseif FirstOfGroup(g)!=null and (udg_EMTarget== null or GetWidgetLife(udg_EMTarget)<=0.405) then
            set udg_EMTarget = FirstOfGroup(g)
            if IsUnitType(udg_EMTarget,UNIT_TYPE_HERO)==true then
                call ALICEText(5.0, "EM Satellites have acquired target : |cff1e90ff" + GetUnitName(udg_EMTarget) + "|r" )
            else
                call ALICEText(5.0, "EM Satellites have acquired target : " + GetUnitName(udg_EMTarget) )
            endif
            set x = GetUnitX(udg_EMTarget)
            set y = GetUnitY(udg_EMTarget)
            //call SetCameraQuickPosition(x,y)
            call PingMinimapEx(x,y,5.0,98,115,202,false)
        endif
        call RemoveLocation(l)
        call DestroyGroup(g)
        call DestroyGroup(g2)
        set l = null
        set l2 = null
        set g = null
        set g2 = null
        set u = null
        set u2 = null
    endfunction


    private function init takes nothing returns nothing
        local trigger t
        local integer i = 0
        set t = CreateTrigger()
        call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
        call TriggerAddCondition( t, Condition(function EMRadiationConditions) )
        call TriggerAddAction(t,function EMRadiation)
        
        set t = CreateTrigger()
        loop
            exitwhen i > 12
            call TriggerRegisterPlayerChatEvent(t, Player(i), "-cancel", true)
            set i = i + 1
        endloop
        call TriggerAddCondition(t, Condition(function CancelEM))
    endfunction

endlibrary